package JSONUtils;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class JSONUtils {
    private JSONUtils(){}
    public static <T> String result(Enum e , T data){
        ResponseData<T> responseData = new ResponseData<T>();
        responseData.setData(data);
        try {
            Method getCode = e.getClass().getMethod("getCode");
            Method getName = e.getClass().getMethod("getName");
            if (getCode != null && getName != null) {
                responseData.setCode((Integer) getCode.invoke(e));
                responseData.setMessage((String) getName.invoke(e));
//                return JSON.toJSONString(responseData);
                return JSONUtil.toJsonStr(responseData);
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
